/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.platformservice.persistence.base;

import org.proteusframework.core.api.IPlatformDelegate;
import org.proteusframework.core.util.Assert;
import org.proteusframework.platformservice.IExtendedPlatformDelegate;
import org.proteusframework.platformservice.appconfig.IAppConfigService;
import org.proteusframework.platformservice.grammar.DefaultGrammarInspector;
import org.proteusframework.platformservice.grammar.IGrammarInspector;
import org.proteusframework.platformservice.grammar.IGrammarInspectorFactory;
import org.proteusframework.platformservice.persistence.api.IMessageBeanManager;
import org.proteusframework.platformservice.persistence.api.messagebean.IMessageBeanDescriptor;

public abstract class AbstractMessageBeanManager implements IMessageBeanManager
{
    private IAppConfigService appCfg;

    /**
     * Injected platform delegate used to access platform services, including locating and instantiating other
     * Proteus objects.
     */
    private IPlatformDelegate delegate = null;
    private IGrammarInspectorFactory grammarInspectorFactory;

    /**
     * {@inheritDoc}
     */
    @Override
    public final <T extends IPlatformDelegate> void setPlatformDelegate(final IPlatformDelegate delegate,
                                                                        Class<T> delegateInterface)
    {
        Assert.unitialized(this.delegate, "Application platform delegate is implicitly final and cannot be modified.");
        Assert.parameterNotNull(delegate, "Parameter 'delegate' must not be null");

        IExtendedPlatformDelegate extendedDelegate = delegate.adapt(IExtendedPlatformDelegate.class);
        appCfg = extendedDelegate.getAppConfigService();

        this.delegate = delegate;

        onDelegateInjected();
    }

    protected void onDelegateInjected()
    {

    }

    protected IAppConfigService getAppConfigService()
    {
        return appCfg;
    }

    protected IExtendedPlatformDelegate getDelegate()
    {
        return delegate.adapt(IExtendedPlatformDelegate.class);
    }

    @Override
    public final void setGrammarInspectorFactory(IGrammarInspectorFactory factory)
    {
        Assert.parameterNotNull(factory, "Parameter 'factory' must not be null");
        this.grammarInspectorFactory = factory;
    }

    @Override
    public final IGrammarInspector createGrammarInspector(IMessageBeanDescriptor messageBeanDescriptor)
    {
        if (null != grammarInspectorFactory)
        {
            return grammarInspectorFactory.createInspector(messageBeanDescriptor);
        } else
        {
            return new DefaultGrammarInspector(messageBeanDescriptor);
        }
    }
}
